package com.mouliu.base.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author liu.mou
 */
public class UseCondition {
    public static void main(String[] args){
        final ReentrantLock reentrantLock =
                new ReentrantLock();
        final Condition condition = reentrantLock.newCondition();
        Thread thread = new Thread((Runnable)()->{
            try {
                reentrantLock.lock();
                System.out.println("我要等一个新信号" + "01");
                condition.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("拿到一个信号！！" + "02");
        },"waitThread1");

        thread.start();

        Thread thread1 = new Thread((Runnable)()->{
            reentrantLock.lock();
            System.out.println("我拿到锁了!");
            try {
                Thread.sleep(30000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            condition.signalAll();
            System.out.println("我发了一个信号！！");
            reentrantLock.unlock();
        },"signalThread");
        thread1.start();
    }
}
